#define _CRT_SECURE_NO_WARNINGS

#include"string.h"

namespace cx
{
	const size_t string::npos = -1;
	string::iterator string::begin()
	{
		assert(_size > 0);
		return _str;
	}

	string::iterator string::end()
	{
		assert(_size > 0);
		return _str + _size;
	}
	string::const_iterator string::begin() const
	{
		assert(_size > 0);
		return _str;
	}

	string::const_iterator string::end() const
	{
		assert(_size > 0);
		return _str + _size;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() const
	{
		return _capacity;
	}


	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1];
		strcpy(_str, str);
		_capacity = _size;
	}

	string::string(const string& s)
	{
		_str = new char[s._size + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;


	}

	string& string::operator=(const string& s)
	{
		/*if (this != &s)
		{
			delete[] _str;
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}*/

		string tmp(s);
		swap(tmp);


		return *this;
	}


	string::string(size_t n, char c)
	{
		_str = new char[n + 1];
		for (size_t i = 0; i < n; ++i)
		{
			_str[i] = c;
		}
		_str[n] = '\0';
		_size = n;
		_capacity = n;
	}

	string:: ~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}


	char& string::operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}

	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}



	}

	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newcapacity);
		}

		_str[_size] = c;
		_size++;
		_str[_size] = '\0';
	}


	void string::append(const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}

		strcat(_str+_size, s);
		_size += len;
	}

	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}


	string& string::operator+=(const char* s)
	{
		append(s);
		return *this;
	}

	void string::insert(size_t pos, char c)
	{
		assert(pos >= 0);
		assert(pos <= _size);
		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newcapacity);
		}

		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end-1];
			--end;
		}
		_str[pos] = c;
		_size++;

	}
	void string::insert(size_t pos, const char* s)
	{
		assert(pos >= 0);
		assert(pos <= _size);
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}
		size_t end = _size;
		while (end >= pos)
		{
			_str[end+len] = _str[end];
			--end;
		}
		memcpy(_str + pos, s, sizeof(char) * len);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos >= 0);
		assert(pos < _size);

		if (pos + len >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			while (pos + len <= _size)
			{
				_str[pos] = _str[pos + len];
				pos++;
			}
			_size -= len;
		}

	}

	size_t string::find(char ch, size_t pos)
	{
		assert(pos >= 0 && pos < _size);
		
		for (size_t i = pos; i < _size; ++i)
		{
			if (_str[i] == ch)
				return i;
		}

		return npos;
	}
	size_t string::find(const char* str, size_t pos)
	{
		assert(pos >= 0 && pos < _size);

		/*char* tmp = strstr(_str, str);

		if (tmp != nullptr)
		{
			return tmp - _str;
		}
		else
		{
			return -1;
		}*/

		char* p = strstr(_str + pos, str);
		return  p - _str;

	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos >= 0 && pos < _size);

		
		if (pos + len >= _size)
		{
			string sub(_str + pos);
			return sub;
		}
		else
		{
			string sub;
			sub.reserve(len);
			for (size_t i = 0; i < len; ++i)
			{
				sub += _str[pos + i];
			}
			return sub;
		}
	}

	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator>=(const string& s) const
	{
		return *this > s || *this == s;
	}
	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch = 0;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}


	ostream& operator<<(ostream& out, string& s)
	{
		for (auto e : s)
		{
			out << e;
		}
		return out;
	}

}